package com.yc.jpyy.base.mobile.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.jspsmart.upload.SmartUpload;
import com.yc.jpyy.base.config.Config;
import com.yc.jpyy.mobile.dao.BizDao;

/**
 * 提交异常文件
 * 
 * @author liufeng
 * @date 2013-11-12
 */
@Component("sendExceptionServlet")
public class SendExceptionServlet extends HttpServlet {
	private static final long serialVersionUID = -1847238807216447030L;
	
	@Autowired
	private BizDao bizDao;
	
	@Override
	public void init() throws ServletException {
		
//		ServletContext servletContext = this .getServletContext();   
//        
//        WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);   
//                  
//        bizDao = (BizDao)ctx.getBean("bizDao" );

	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse resp)
			throws ServletException, IOException {

		doPost(request, resp);

	}

	@SuppressWarnings("static-access")
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");

		resp.setContentType("text/html,charset=UTF-8");

		SmartUpload smartUpload = new SmartUpload();
		
		try {

			// 添加缓存
			smartUpload.initialize(this.getServletConfig(), req, resp);
			smartUpload.upload();
			com.jspsmart.upload.File smartFile = smartUpload.getFiles()
					.getFile(0);

			if (!smartFile.isMissing()) {

				String path = getServletContext().getRealPath("");
				
				// 保存路径
				String saveFileName = path.substring(0 , path.lastIndexOf("\\") + 1) + Config.getExceptionPath() + "/" + smartFile.getFileName();
				
				
				// 保存异常文件路径
				smartFile.saveAs(saveFileName, smartUpload.SAVE_PHYSICAL);
				
				
				System.out.println(Config.getImageAddress() + smartFile.getFileName());
				resp.getWriter().print("1");
				
				
			} else {
				resp.getWriter().print("0");
			}
		} catch (Exception e) {
			resp.getWriter().print("0");
			e.printStackTrace();
			resp.getWriter().print(e);
		}

	}
}
